package com.woniuxy.order.controller;


import com.woniu.commons.util.JwtUtil;
import com.woniu.zsd.commons.core.model.Result;
import com.woniuxy.order.entity.OrderInfo;
import com.woniuxy.order.service.IOrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import zsd.waste.dto.OrderInfoDto;
import zsd.waste.form.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/order-info")
@Api(tags = "订单操作")
public class OrderInfoController {

    @Resource
    private IOrderInfoService orderInfoService;
    @Resource
    private JwtUtil jwtUtil;

    /**
     * 下单
     * @form form
     * @return
     */
    @PostMapping("/createOrder")
    @ApiOperation("用户下单操作")
    public Result<OrderInfo> createOrder(@RequestHeader String token,@RequestBody CreateOrderForm form)throws Exception{
        String id = jwtUtil.parseToken(token).get("id").toString();
        Integer userId = Integer.parseInt(id);
        form.setUserId(userId);
        OrderInfo orderInfo = orderInfoService.createOrder(form);
        if (orderInfo==null) {
            return Result.getResult(OrderInfo.class).setMsg("下单失败").setCode(500);
        }
        return Result.getResult(OrderInfo.class).setMsg("下单成功").setData(orderInfo);
    }

    /**
     * 用户查询自己订单操作
     *
     * @return
     */
    @GetMapping("/findMyOrder")
    @ApiOperation("用户查询自己订单操作")
    public Result<List<OrderInfoDto>> findMyOrder(@RequestHeader String token,Integer pageNum,Integer pageSize)throws Exception{
        String id = jwtUtil.parseToken(token).get("id").toString();
        Integer userId = Integer.parseInt(id);
        List<OrderInfoDto> list = orderInfoService.findMyOrder(userId,pageNum,pageSize);
        return Result.getListResult(OrderInfoDto.class).setMsg("查询自己订单成功").setData(list);
    }

    /**
     * 管理员查询全部订单操作
     *
     * @return
     */
    @PostMapping("/findAllOrder")
    @ApiOperation("管理员根据条件查询查询订单操作")
    public Result<List<OrderInfoDto>> findAllOrder(@RequestBody FindAllOrderFrom findAllOrderFrom)throws Exception{
        List<OrderInfoDto> list = orderInfoService.findAllOrder(findAllOrderFrom);
        return Result.getListResult(OrderInfoDto.class).setMsg("根据条件查询查询订单成功").setData(list);
    }


    /**
     * 站点根据条件查询自己站点订单操作
     *
     * @return
     */
    @PostMapping("/findSiteOrder")
    @ApiOperation("站点根据条件查询自己站点订单操作")
    public Result<List<OrderInfoDto>> findSiteOrder(@RequestBody FindSiteOrderFrom findSiteOrderFrom,@RequestHeader String token)throws Exception{
        String id = jwtUtil.parseToken(token).get("id").toString();
        Integer userId = Integer.parseInt(id);
        List<OrderInfoDto> list = orderInfoService.findSiteOrder(findSiteOrderFrom,userId);
        return Result.getListResult(OrderInfoDto.class).setMsg("根据条件查询查询订单成功").setData(list);
    }

    /**
     *管理员强制分配工单
     */
    @PostMapping("/siteOrder")
    @ApiOperation("管理员强制分配工单给站点")
    public Result siteOrder(@RequestBody SiteOrderForm wof, @RequestHeader String token)throws Exception{
        //获取管理员id
        String adminId = jwtUtil.parseToken(token).get("id").toString();
        orderInfoService.siteOrder(wof,adminId);
        return Result.getResult().setMsg("分配站点成功");
    }


    /**
    *工单分配临时工
    */
    @PostMapping("/workOrder")
    @ApiOperation("站点分配工单给临时工")
    public Result workOrder(@RequestBody WorkOrderForm form, @RequestHeader String token)throws Exception{
        //获取管理员id
        String adminId = jwtUtil.parseToken(token).get("id").toString();
        Integer userId = Integer.parseInt(adminId);
        orderInfoService.workOrder(form,userId);
        return Result.getResult().setMsg("分配临时工成功");
    }

    /**
     * 站点确认接单
     */
    @PostMapping("/confirmOrder")
    @ApiOperation("站点确认接单操作")
    public Result confirmOrder(@RequestHeader String token, @RequestParam String orderNumber) throws Exception {
        String userId = jwtUtil.parseToken(token).get("id").toString();
        Integer id = Integer.parseInt(userId);
        orderInfoService.confirmOrder(orderNumber, id);
        return Result.getResult().setMsg("接单成功");
    }


    /**
     * 临时工查询订单
     */
    @PostMapping("/temporaryOrderInfo ")
    @ApiOperation("临时工查询订单")
    public Result<List<OrderInfoDto>> temporaryOrderInfo(@RequestHeader String token, Integer pageNum,Integer pageSize) throws Exception {
        String userId = jwtUtil.parseToken(token).get("id").toString();
        Integer id = Integer.parseInt(userId);
        List<OrderInfoDto> list =  orderInfoService.temporaryOrderInfo( id,pageNum,pageSize);
        return Result.getListResult(OrderInfoDto.class).setMsg("查询自己订单成功").setData(list);
    }



}
